<template>
  <div>
    <el-form ref="form" :model="form" label-width="120px" v-if="this.user.username === undefined">
      <el-form-item label="门店名称">
        <el-input v-model="form.shopName" placeholder="请输入门店名称"></el-input>
      </el-form-item>
      <el-form-item label="门店地址">
        <el-input v-model="form.shopAddress" placeholder="请输入门店地址"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button  @click="add" type="success" round>新增门店</el-button>
      </el-form-item>
    </el-form>
    <el-divider v-if="this.user.username === undefined"></el-divider>
    <el-table :data="shops" border>
      <el-table-column prop="shopName" label="店铺名称">
      </el-table-column>
      <el-table-column prop="shopAddress" label="店铺地址">
      </el-table-column>
      <el-table-column width="90%">
        <template slot-scope="scope">
          <el-button type="primary" round @click="toShopInfo(scope.row.shopId)">前往</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>


<script>
import {getSessionStorage} from "@/common";
import axios from "axios";
import router from "@/router";
import qs from "qs";

export default {
  data() {
    return {
      shops: [],
      user: {},
      form:{}
    }
  },
  created() {
    this.user = getSessionStorage('user')
    console.log(this.user)
    if(this.user.username !== undefined){
      axios.get('/shop/shopList').then(response => {
        this.shops = response.data
      })
    }else{
      axios.post('/shop/listShopsByBusinessId', qs.stringify({
        businessId: this.user.businessId
      })).then(response => {
        this.shops = response.data
      })
    }
  },
  methods: {
    toShopInfo(shopId){
      if(this.user.username !== undefined){
        router.push({
          path: '/shopInfo',
          query: {
            shopId: shopId
          }
        })
      }else{
        router.push({
          path:'/itemManage',
          query:{
            shopId: shopId
          }
        })
      }
    },
    add(){
      axios.post("/shop/save", qs.stringify({
        businessId: this.user.businessId,
        shopName: this.form.shopName,
        shopAddress: this.form.shopAddress
      })).then(() => {
        alert('门店添加成功')
      })
    },
  }
}
</script>

<style>
.el-table td.el-table-column--center {
  text-align: center;
}
</style>


